﻿; This file contains the data to create a new issue or comment (as well as the issue gui)

new_issue_gui()
{

; Get global variable for language
global language
global new_issue_win_title
; Grab language strings
; Ex: get_language_string("types",language)

; Only allow one new issue window at a time
; This reduces the chance of duplicate issue IDs
IfWinExist, %new_issue_win_title%
{
	WinActivate, %new_issue_win_title%
	Exit
}

; No issue window? Great!
IfWinNotExist, %new_issue_win_title%
{
	Gui 3:+LabelNewIssue
	
	; Get the strings to use
	type := get_language_string("type",language)
	priority := get_language_string("priority",language)
	types := get_language_string("types",language)
	priorities := get_language_string("priorities",language)
	
	subject := get_language_string("subject",language)
	includefirstcomment := get_language_string("includefirstcomment",language)
	
	addissue := get_language_string("addissue",language)
	close := get_language_string("close",language)
	
	; Build type list
	; Typelist is a CSV, so we'll loop through values to build our filter list
	typelist =
	num_types = 1
	Loop,parse, types,`,
	{
		; If it's not a blank value
		If (A_Loopfield != "") 
		{	
			; Increment the counter that sets the dropdownlist height (number of options shown without scrolling)
			num_types += 1 
			
			If (A_Index = "1") ; Make the first item the selected one
				typelist=%typelist%%A_loopfield%||  ; Add it to our list
			Else
				typelist=%typelist%%A_loopfield%|  ; Add it to our list
		}
	}
	
	; Build priority list
	prioritylist =
	num_priorities = 1
	Loop,parse, priorities,`,
	{
		; If it's not a blank value
		If (A_Loopfield != "") 
		{
			num_priorities += 1 ; Increment the counter that sets the dropdownlist height (number of options shown without scrolling)
			
			If (A_Index = "2") ; Select second item (Normal)
				prioritylist=%prioritylist%%A_loopfield%||  ; Add it to our list
			Else
				prioritylist=%prioritylist%%A_loopfield%|
		}
	}
	
	; All gui statements (such as adding items) until "gui,1:default" will operate on gui #3
	Gui,3:Default
	
	; Category/priority selection
	Gui, Add, Text, x12 y13 w80 h30 , %type%:
	Gui, Add, Text, x12 y53 w80 h30 , %priority%:
	Gui, Add, DropDownList, x92 y10 w100 h20 r%num_types%, %typelist%
	Gui, Add, DropDownList, x92 y50 w100 h20 r%num_priorities%, %prioritylist%

	; Subject
	Gui, Add, Text, x12 y93 w80 h30 , %subject%:
	Gui, Add, Edit, x92 y90 w260 h20 ,

	; Optional initial comment
	Gui, Add, CheckBox, x12 y140 w130 h30 gnew_issue_checkbox, %includefirstcomment%
	Gui, Add, Edit, x12 y170 w340 h40 ,
	
	; Buttons
	Gui, Add, Button, x12 y230 w120 h40 gaddissue, %addissue%
	Gui, Add, Button, x232 y230 w120 h40 gNewIssueClose, %close%
	
	; Generated using SmartGUI Creator 4.0
	Gui, Show, h290 w370, %new_issue_win_title%
	
	; Disable optional comment box (can be toggled later)
	Control,Disable,,Edit2,%new_issue_win_title%
	
	Gui,1:Default
	Return
	
	
	; Enable/disable optional comment field
	new_issue_checkbox:
	ControlGet,new_issue_CB,Checked,,Button1,%new_issue_win_title%
	If new_issue_CB
		Control,Enable,,Edit2,%new_issue_win_title%
	Else
		Control,Disable,,Edit2,%new_issue_win_title%
	Return
	
	; If the add issue button is pressed
	addissue: 
	write_new_issue() ; Add the issue, then exit
	list_issues()
	
	; On close
	NewIssueClose:
	Gui,3: Destroy ; Free the memory
	Return
}
}

; Pulls info from the GUI and writes it to both INIs (RAM/HD)
write_new_issue()
{
; Access global variables
global issues
global languages
global data_dir

; Get Drop down lists/subject line
ControlGet,issue_type,Choice,,combobox1,%new_issue_win_title%
ControlGet,issue_priority,Choice,,combobox2,%new_issue_win_title%
ControlGetText,issue_subject,edit1,%new_issue_win_title%

; Get Optional comment
ControlGet,comment_CB,Checked,,Button1,%new_issue_win_title%
ControlGetText,issue_firstcomment,edit2,%new_issue_win_title%

; Get current issue values (to be updated for this one)
issue_num := ini_getvalue(issues,"issues","number")
open_issues := ini_getvalue(issues,"issues","open")
new_issue_num := issue_num + 1
open_issues = %open_issues%%new_issue_num%,

; Get language strings
initialstatus := get_language_string("initialstatus",language)
;Msgbox, %issue_num%

; debug=
; (
; Type: %issue_type%
; Priority: %issue_priority%
; Subject: %issue_subject%

; Include Comment: %comment_CB%
; Comment: %issue_firstcomment%
; )
; Msgbox, %debug%

; Write new issue information to RAM
new_issue_data=status=%initialstatus%`ntype=%issue_type%`nsubject=%issue_subject%`npriority=%issue_priority%`ncommentIDs=`ndate=%A_Now%
;msgbox, %newissuedata%

; Add issue
ini_insertSection(issues,"issue"new_issue_num,new_issue_data)

; Update open list/issue total
ini_replaceValue(issues,"issues","open",open_issues)
ini_replaceValue(issues,"issues","number",new_issue_num)

; Write new issue information to HD
; Set path as a var to shorten/simplify other statements
issues_ini=%data_dir%\Issues\issues.ini
new_issue_ini=%data_dir%\Issues\issue%new_issue_num%.ini

; Issue data
temp_issue=
ini_insertSection(temp_issue,"issue"new_issue_num,new_issue_data)
ini_save(temp_issue,new_issue_ini)

; Temp/Open/number updates
IniRead,temp_list,%issues_ini%,issues,temp
IniWrite,%temp_list%%new_issue_num%`,,%issues_ini%,issues,temp
IniWrite,%new_issue_num%,%issues_ini%,issues,number
IniWrite,%open_issues%,%issues_ini%,issues,open

If (comment_CB) ; If they've elected to include a comment
	new_comment(issue_firstcomment,new_issue_num)
}

; This function adds a comment to the specified issue
new_comment(comment,IssueID)
{

; Load language strings
global language
global comments
global issues
global data_dir


; Get/determine commentID
comment_num := ini_getvalue(comments,"comments","number")
temp_comments := ini_getvalue(comments,"comments","temp")
commentIDs := ini_getvalue(issues,"issue"issueID,"commentIDs")
comment_num +=1
comment_data=date=%A_Now%`nissue=%issueID%`ncomment=%comment%

; Insert comment data into RAM, update num/temp
temp_comments = %temp_comments%%comment_num%,
commentIDs = %comment_num%,%commentIDs%
ini_insertSection(comments,"comment"comment_num,comment_data)
ini_replaceValue(comments,"comments","number",comment_num)
ini_replaceValue(comments,"comments","temp",temp_comments)
ini_replaceValue(issues,"issue"issueID,"commentIDs",commentIDs)

; Write to temp file (HD)
; Set path as a var to shorten/simplify other statements
comments_ini=%data_dir%\Issues\comments.ini
temp=%data_dir%\Issues\issue%IssueID%.ini
If FileExist(temp) ; If the issue is a temp file
	issues_ini=%data_dir%\Issues\issue%IssueID%.ini ; Write the data there instead
Else
	issues_ini=%data_dir%\Issues\issues.ini
new_comment_ini=%data_dir%\Issues\comment%comment_num%.ini


temp_comment=
ini_insertSection(temp_comment,"comment"comment_num,comment_data)
ini_save(temp_comment,new_comment_ini)

IniWrite,%temp_comments%,%comments_ini%,comments,temp
IniWrite,%comment_num%,%comments_ini%,comments,number
IniWrite,%commentIDs%,%issues_ini%,issue%issueID%,commentIDs
Return
}

